﻿using BaiduRules;
using RulesEngine;

namespace NUnitTest.VipRules.Validation.Rules
{
    public static class ExtensionMethods
    {
        public static bool SetValidateResult(this INewVipRule rule, bool result, INewVipCandidate candidate)
        {
            rule.IsPass = result;
            DateTime expiredDate = candidate.DebuteDate.AddMonths(rule.ForwardOffsetMonth);
            int offsetDays = (candidate.EffectiveDate - expiredDate).Days;
            if (!result && offsetDays >= 0)
            {
                //rule.QualifiedToPay = VipVaidateResult.Expired;
            }
            else if (!result && offsetDays < 0)
            {
                //rule.QualifiedToPay = VipVaidateResult.RuleSetFalse;
            }
            else
            {
                //rule.QualifiedToPay = VipVaidateResult.RuleSetSuccess;
            }
            return result;
        }

        private static void GetRules(IRule<INewVipCandidate> rule, List<IRule<INewVipCandidate>> list)
        {
            var tempRule = rule as AndRule<INewVipCandidate>;
            if (tempRule != null)
            {
                list.Add(tempRule.Other);
                if (tempRule.One is AndRule<INewVipCandidate>)
                {
                    GetRules(tempRule.One, list);
                }
                else
                {
                    list.Add(tempRule.One);
                }
            }
            else
            {
                list.Add(rule);
            }
        }

        public static List<IRule<INewVipCandidate>> GetRuleList
        (
            this RulesEngine<INewVipCandidate> engine,
            string name,
            INewVipCandidate candidate
        )
        {
            engine.ExecuteRuleSet(name, candidate.EffectiveDate, candidate);

            var ruleSet = engine.GetRuleSet(name, candidate.EffectiveDate);
            var rule = ruleSet.Groups[0].Rules[0];
            var outlist = new List<IRule<INewVipCandidate>>();
            GetRules(rule, outlist);
            return outlist;
        }

        public static VipVaidateResult ExecuteCustomRuleSet(
                this RulesEngine<INewVipCandidate> engine,
                string name,
                INewVipCandidate candidate
            )
        {
            if (engine.ExecuteRuleSet(name, candidate.EffectiveDate, candidate).Result)
            {
                return VipVaidateResult.RuleSetSuccess;
            }
            else
            {
                var ruleSet = engine.GetRuleSet(name, candidate.EffectiveDate);
                var rootRule = ruleSet.Groups[0].Rules[0];
                List<IRule<INewVipCandidate>> list = new List<IRule<INewVipCandidate>>();
                GetRules(rootRule, list);
                var ruleList = from o in list
                               where o is INewVipCandidate
                               select o;

                foreach (var rule in ruleList)
                {
                    if (!rule.Execute(candidate)) //&& (rule as INewVipRule).QualifiedToPay == VipVaidateResult.Expired
                    {
                        return VipVaidateResult.Expired;
                    }
                }

                //CurrentCompetitionMonth 6:
                var month = GetMonthCount(candidate);
                if (month >= 5) //过期： 第6-12个月 只要最近12个月有书面以上警告 / 不是A，并且有培训记录
                {
                    var activityStatus = ruleList.Where(c => c is VipLevelRule).First().Execute(candidate);
                    var trainStatus = ruleList.Where(c => c is VipStatusRule).First().Execute(candidate);
                    if (!activityStatus && trainStatus)
                    {
                        return VipVaidateResult.Expired;
                    }
                }

                return VipVaidateResult.RuleSetFalse;
            }
        }

        private static int GetMonthCount(INewVipCandidate candidate)
        {
            return (candidate.EffectiveDate.Year - candidate.DebuteDate.Year) * 12 + candidate.EffectiveDate.Month - candidate.DebuteDate.Month;
        }
    }
}